<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class FaceController extends BaseController{
	//添加粉丝
	public function add_get(){
		$data['customerid']=$_GET['fansid'];
		$data['fansid']=$_SESSION['customer']['id'];	
		$res=$this->isExist($data['fansid'],$data['customerid']);		
		if($res){		
			$data=array(
			 	'error_no'=>20007,
			 	'msg'=>'已关注过',	
			 	'status'=>0,	
			);	
		}else{
			$res=M('CustomerFans')->add($data);
			if($res){
				$map['id']=$_GET['fansid'];
				M('Customer')->where($map)->setInc('fanscount');
				$this->addFanscount($_GET['fansid'],1);
				$data=array(
					'error_no'=>10007,
					'msg'=>'关注成功',
					'status'=>1,
				);
			}
		}
		
		$this->Response($data,'json');
	}
	/**
	*  <code>
	*	检测粉丝是否已经存在
	*  </code>
	*  @params int $customerid 
	* 	@params int $fansid 
	*
	*/

	public function isExist($customerid,$fansid){
		$map['customerid']=$fansid;
		$map['fansid']=$customerid;	
		return M('CustomerFans')->where($map)->find();
		
		
	}
	public function addCount($customerid,$addCount=1){	
		$data['customerid']=$customerid;
		$data['fanscount']=$addCount;
		$data['message']="你有新的关注信息";	
		$M('Notice')->add($data);

	}
	/*
		<code>
			删除粉丝
		</code>

	*/

	public function del_get_json(){		
		$map['customerid']=$_GET['customerid'];
		$map['fansid']=$_SESSION['customer']['id'];			
		$res=M('CustomerFans')->where($map)->delete();			
		M('Notice')->where(array('customerid'=>$_GET['customerid']))->delete();
		if($res){		
			$data=array(
				'error_no'=>10008,
				'msg'=>'取消关注成功',
				'status'=>1,
			);
		}else{
			$data=array(
				'error_no'=>20008,
				'msg'=>'取消关注失败',
				'status'=>0,
			);
		}
		$this->Response($data,'json');

	}

}